/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div
      id="explanation"
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>发送图片消息请移步粉丝列表</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>
    <div class="mDiv">
      <div class="ftitle">
        <h3>图片列表</h3>
        <h5>(共 {{totalCount}} 条记录)</h5>
      </div>
      <el-upload
        class="avatar-uploader fbutton"
        accept=".gif, .jpg, .jpeg, .bmp, .png"
        :action="apiHead + '/mall/upload/wechat/media?type=image'"
        :show-file-list="false"
        :on-success="handleSuccess"
        :before-upload="beforeUpload"
      >
        <div class="add">
          <span>
            <i class="el-icon-circle-plus" /> 上传
          </span>
        </div>
      </el-upload>
      <a href @click.prevent="refresh()" class="refresh-date">
        <div title="刷新数据" class="pReload">
          <i class="fa fa-refresh"></i>
        </div>
      </a>
    </div>
    <div class="tablebox">
      <div class="ma-list">
        <div v-if="!images" class="no-data">
          <i class="fa fa-exclamation-circle"></i>还没有相关素材~
        </div>
        <div class="ma-card" v-for="img in images">
          <div class="news-border">
            <div class="title ellipsis-1">{{img.name}}</div>
            <div class="time">{{img.update_time | datefmtAll}}</div>
            <div class="card-item no-line">
              <el-image
                :src="img.path"
                :preview-src-list="[img.path]"
                fit="scale-down"
                style="height:200px"
              />
            </div>
            <div class="btns">
              <div class="btn" title="删除图片素材" @click="remove(img.id)">
                <span class="fa fa-trash"></span>
              </div>
            </div>
          </div>
        </div>
      </div>
      <el-pagination
        background
        class="paginationbox"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { getMaterialPage } from "@/utils/api";
export default {
  data() {
    return {
      images: [],
      currentPage: this.$route.params.currentPage || 1,
      totalCount: 0,
      pageSize: 10,
      currentDate: new Date(),
    };
  },
  created() {
    this.getPage();
  },
  methods: {
    handleSizeChange(e) {
      this.pageSize = e;
      this.getPage();
    },
    handleCurrentChange(e) {
      this.currentPage = e;
      this.getPage();
    },
    getPage() {
      var that = this;
      getMaterialPage({
        type: "image",
        page: that.currentPage,
        size: that.pageSize,
      }).then(function (res) {
        if (res.msg) {
          that.$message.error(res.msg);
        } else {
          that.images = res.records;
          that.totalCount = res.total;
        }
      });
    },
    refresh() {
      this.getPage();
      this.$message.success("刷新成功！");
    },
    remove(id) {
      var that = this;
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          that.request.delete("/mall/wx_material?id=" + id).then((res) => {
            if (res.status == 1) {
              that.getPage();
              that.$message.success("删除成功!");
            } else {
              that.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          that.$message.info("已取消删除");
        });
    },
    handleSuccess(res) {
      if (res.status) {
        this.$message.error(res.msg);
        return;
      }
      this.$message.success("上传成功");
      this.getPage();
    },
    beforeUpload(file) {
      const isLt10M = file.size / 1024 / 1024 < 10;
      if (!isLt10M) {
        this.$message.error("上传图片大小不能超过 10MB!");
      }
      return isLt10M;
    },
  },
};
</script>

<style scoped>
@import "../../../../static/css/weixin-mp.css";
.tablebox {
  float: left;
}
>>> .avatar-uploader .el-upload {
  border: none;
}
.ma-card .btns .btn {
  width: 100%;
}
</style>
